package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.qo.CustomerQueryObject;
import cn.wolfcode.crm.qo.JsonResult;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @RequestMapping("/potentialList")
    //@RequiresPermissions(value={"潜在客户列表","customer:list"},logical= Logical.OR)
    public String potentialList(@ModelAttribute("qo") CustomerQueryObject qo, Model model){
        //设置qo字段为潜在用户,Mapper查询使用
        qo.setStatus(Customer.STATUS_POTENTIAL);
        //获取当前用户,     判断用户是超管/市场经理,否则只显示自己的数据
        Subject subject = SecurityUtils.getSubject();
        Employee currentEmployee = (Employee) subject.getPrincipal();
        //判断用户的角色,在判断登录的时候就已经放了角色信息
        if(subject.hasRole(Role.ROLE_SN_ADMIN) || subject.hasRole(Role.ROLE_SN_MARKET_MANAGER)){
            qo.setSellerId(-1L);
        }else{
            qo.setSellerId(currentEmployee.getId());
        }

        //数据库取用户
        PageInfo<Customer> pageResult = customerService.selectByQuery(qo);
        //List<Customer> list = customerService.getAll();
        model.addAttribute("result",pageResult);
        //高查里的销售人员列表,  role里面写了静态字段,可以调用
        List<Employee> sellers = employeeService.queryByRoleSn(new String[]{Role.ROLE_SN_MARKET_MANAGER,Role.ROLE_SN_MARKET});
        model.addAttribute("sellers",sellers);

        //拿到所有工作,给添加页面使用--job
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectBySystemDictionarySn("job");
        model.addAttribute("jobs",jobs);
        //拿到所有来源--source
        List<SystemDictionaryItem> sources = systemDictionaryItemService.selectBySystemDictionarySn("source");
        model.addAttribute("sources",sources);
        List<SystemDictionaryItem> ccts = systemDictionaryItemService.selectBySystemDictionarySn("communicationMethod");
        model.addAttribute("ccts",ccts);
        return "customer/potential_list";
    }



//    @RequestMapping("/input")
//    @RequiredPermission({"潜在客户添加","customer:input"})
//    public String input(Long id, Model model){
//        if(id != null){
//            Customer customer = customerService.getCustomerById(id);
//            model.addAttribute("entity",customer);
//        }
//        return "customer/input";
//    }
    @RequestMapping("/saveOrUpdate")
    //@RequiresPermissions(value={"潜在客户保存或更新","customer:saveOrUpdate"},logical= Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Customer customer){
        try {
            customerService.saveOrUpdate(customer);
            return new JsonResult();
        }catch(Exception e){
            e.printStackTrace();
            return new JsonResult(false,"保存失败");
        }
    }
    @RequestMapping("/updateStatus")
    public String updateStatus(Long customerId,Integer status){
        customerService.updateStatus(customerId,status);
        return "redirect:/customer/potentialList.do";
    }




    @RequestMapping("/poolList")
    //@RequiresPermissions(value={"潜在客户列表","customer:list"},logical= Logical.OR)
    public String poolList(@ModelAttribute("qo") CustomerQueryObject qo, Model model){
        //设置qo字段为潜在用户,Mapper查询使用
        qo.setStatus(Customer.STATUS_POOL);

        //数据库取用户
        PageInfo<Customer> pageResult = customerService.selectByQuery(qo);
        //List<Customer> list = customerService.getAll();
        model.addAttribute("result",pageResult);
        //高查里的销售人员列表,  role里面写了静态字段,可以调用
        List<Employee> sellers = employeeService.queryByRoleSn(new String[]{Role.ROLE_SN_MARKET_MANAGER,Role.ROLE_SN_MARKET});
        model.addAttribute("sellers",sellers);

        //拿到所有工作,给添加页面使用--job
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectBySystemDictionarySn("job");
        model.addAttribute("jobs",jobs);
        //拿到所有来源--source
        List<SystemDictionaryItem> sources = systemDictionaryItemService.selectBySystemDictionarySn("source");
        model.addAttribute("sources",sources);
        List<SystemDictionaryItem> ccts = systemDictionaryItemService.selectBySystemDictionarySn("communicationMethod");
        model.addAttribute("ccts",ccts);
        return "customer/pool_list";
    }


    @RequestMapping("/list")
    //@RequiresPermissions(value={"潜在客户列表","customer:list"},logical= Logical.OR)
    public String list(@ModelAttribute("qo") CustomerQueryObject qo, Model model){
        //qo.setStatus(Customer.STATUS_ALL);
        //数据库取用户
        PageInfo<Customer> pageResult = customerService.selectByQuery(qo);
        //List<Customer> list = customerService.getAll();
        model.addAttribute("result",pageResult);
        //高查里的销售人员列表,  role里面写了静态字段,可以调用
        List<Employee> sellers = employeeService.queryByRoleSn(new String[]{Role.ROLE_SN_MARKET_MANAGER,Role.ROLE_SN_MARKET});
        model.addAttribute("sellers",sellers);

        //拿到所有工作,给添加页面使用--job
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectBySystemDictionarySn("job");
        model.addAttribute("jobs",jobs);
        //拿到所有来源--source
        List<SystemDictionaryItem> sources = systemDictionaryItemService.selectBySystemDictionarySn("source");
        model.addAttribute("sources",sources);
        List<SystemDictionaryItem> ccts = systemDictionaryItemService.selectBySystemDictionarySn("communicationMethod");
        model.addAttribute("ccts",ccts);
        return "customer/list";
    }
    @RequestMapping("/failList")
    //@RequiresPermissions(value={"潜在客户列表","customer:list"},logical= Logical.OR)
    public String failList(@ModelAttribute("qo") CustomerQueryObject qo, Model model){
        qo.setStatus(Customer.STATUS_FAILURE);
        //数据库取用户
        PageInfo<Customer> pageResult = customerService.selectByQuery(qo);
        //List<Customer> list = customerService.getAll();
        model.addAttribute("result",pageResult);
        //高查里的销售人员列表,  role里面写了静态字段,可以调用
        List<Employee> sellers = employeeService.queryByRoleSn(new String[]{Role.ROLE_SN_MARKET_MANAGER,Role.ROLE_SN_MARKET});
        model.addAttribute("sellers",sellers);

        //拿到所有工作,给添加页面使用--job
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectBySystemDictionarySn("job");
        model.addAttribute("jobs",jobs);
        //拿到所有来源--source
        List<SystemDictionaryItem> sources = systemDictionaryItemService.selectBySystemDictionarySn("source");
        model.addAttribute("sources",sources);
        List<SystemDictionaryItem> ccts = systemDictionaryItemService.selectBySystemDictionarySn("communicationMethod");
        model.addAttribute("ccts",ccts);
        return "customer/fail_list";
    }



    @RequestMapping("/lostList")
    //@RequiresPermissions(value={"潜在客户列表","customer:list"},logical= Logical.OR)
    public String lostList(@ModelAttribute("qo") CustomerQueryObject qo, Model model){
        qo.setStatus(Customer.STATUS_LOST);
        //数据库取用户
        PageInfo<Customer> pageResult = customerService.selectByQuery(qo);
        //List<Customer> list = customerService.getAll();
        model.addAttribute("result",pageResult);
        //高查里的销售人员列表,  role里面写了静态字段,可以调用
        List<Employee> sellers = employeeService.queryByRoleSn(new String[]{Role.ROLE_SN_MARKET_MANAGER,Role.ROLE_SN_MARKET});
        model.addAttribute("sellers",sellers);

        //拿到所有工作,给添加页面使用--job
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectBySystemDictionarySn("job");
        model.addAttribute("jobs",jobs);
        //拿到所有来源--source
        List<SystemDictionaryItem> sources = systemDictionaryItemService.selectBySystemDictionarySn("source");
        model.addAttribute("sources",sources);
        List<SystemDictionaryItem> ccts = systemDictionaryItemService.selectBySystemDictionarySn("communicationMethod");
        model.addAttribute("ccts",ccts);
        return "customer/lost_list";
    }

    @RequestMapping("/fomalList")
    //@RequiresPermissions(value={"潜在客户列表","customer:list"},logical= Logical.OR)
    public String fomalList(@ModelAttribute("qo") CustomerQueryObject qo, Model model){
        //设置qo字段为潜在用户,Mapper查询使用
        qo.setStatus(Customer.STATUS_FOMAL);
        //获取当前用户,     判断用户是超管/市场经理,否则只显示自己的数据
        Subject subject = SecurityUtils.getSubject();
        Employee currentEmployee = (Employee) subject.getPrincipal();
        //判断用户的角色,在判断登录的时候就已经放了角色信息
        if(subject.hasRole(Role.ROLE_SN_ADMIN) || subject.hasRole(Role.ROLE_SN_MARKET_MANAGER)){
            qo.setSellerId(-1L);
        }else{
            qo.setSellerId(currentEmployee.getId());
        }

        //数据库取用户
        PageInfo<Customer> pageResult = customerService.selectByQuery(qo);
        //List<Customer> list = customerService.getAll();
        model.addAttribute("result",pageResult);
        //高查里的销售人员列表,  role里面写了静态字段,可以调用
        List<Employee> sellers = employeeService.queryByRoleSn(new String[]{Role.ROLE_SN_MARKET_MANAGER,Role.ROLE_SN_MARKET});
        model.addAttribute("sellers",sellers);

        //拿到所有工作,给添加页面使用--job
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectBySystemDictionarySn("job");
        model.addAttribute("jobs",jobs);
        //拿到所有来源--source
        List<SystemDictionaryItem> sources = systemDictionaryItemService.selectBySystemDictionarySn("source");
        model.addAttribute("sources",sources);
        List<SystemDictionaryItem> ccts = systemDictionaryItemService.selectBySystemDictionarySn("communicationMethod");
        model.addAttribute("ccts",ccts);
        return "customer/fomal_list";
    }
}
